from sqlalchemy import select, update
from domain.models.models import  User
from db.db import async_session
from common.tencent_im import TencentIM
import json

async def check_user_info(unionid: str) -> bool:
    async with async_session() as session:
        result = await session.execute(
            select(User).where(
                User.wechat_id == unionid,
                User.is_delete == 0
            )
        )
        return result.scalar_one_or_none() is not None

async def create_new_user(unionid: str) -> tuple[bool, int]:
    mask_info = {"age": True, "profession": True, "city": True}
    new_user = User(
        wechat_id=unionid,
        points=10,
        mask_info=json.dumps(mask_info)
    )
    
    async with async_session() as session:
        try:
            session.add(new_user)
            await session.flush()  # 获取自增ID
            user_id = new_user.id
            # 向腾讯云注册
            im = TencentIM()
            register_result = await im.register_user(str(user_id))
            if not register_result[0]:
                return False, 0
            await session.commit()
            return True, user_id
        except Exception as e:
            await session.rollback()
            return False, 0
async def get_user_by_id(user_id: int):
    """根据用户ID获取用户信息"""
    async with async_session() as session:
        result = await session.execute(
            select(User).where(
                User.id == user_id,
                User.is_delete == 0
            )
        )
        user = result.scalar_one_or_none()
        if user:
            return True, {
                "id": user.id,
                "name": user.name,
                "phone_number": user.phone_number,
                "age": user.age,
                "profession": user.profession,
                "city": user.city,
                "interests": user.interests,
                "points": user.points,
                "is_auth": user.is_auth,
                "photo_url": user.photo_url,
                "mask_info": user.mask_info
            }
        return False, None
async def get_user_info_by_wechat_id(unionid: str):
    async with async_session() as session:
        result = await session.execute(
            select(User).where(
                User.wechat_id == unionid,
                User.is_delete == 0
            )
        )
        user = result.scalar_one_or_none()
        if user:
            return True, {
                "id": user.id,
                "name": user.name,
                "wechat_id": user.wechat_id,
                "phone_number": user.phone_number,
                "age": user.age,
                "profession": user.profession,
                "city": user.city,
                "interests": user.interests,
                "points": user.points,
                "is_auth": user.is_auth,
                "photo_url": user.photo_url,
                "mask_info": user.mask_info
            }
        return False, None

async def update_girls_info(id: str, body: dict):
    async with async_session() as session:
        try:
            stmt = (
                update(User)
                .where(User.id == id, User.is_delete == 0)
                .values(**{k: v for k, v in body.items() if k != 'id'})
            )
            result = await session.execute(stmt)
            await session.commit()
            if result.rowcount == 0:
                return False, "Girl record not found"
            return True, result.rowcount
        except Exception as e:
            return False, str(e)